﻿using MCPlatform.MotionLib.Base.AssitantClass;
using MCPlatform.MotionLib.Base.Environment;
using Newtonsoft.Json;
using Panuon.WPF;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MCPlatform.MotionLib.Base.Models
{
    public class ImpMotionParaModel : NotifyPropertyChangedBase, INameProvider
    {

        #region Properties

        [JsonIgnore]
        public IImputation? CrdEntity
        {
            get => _CrdEntity;
            set
            {
                Set(ref _CrdEntity, value);

            }
        }
        [JsonIgnore]
        private IImputation? _CrdEntity;



        /// <summary>
        /// 坐标系号名称
        /// </summary>

        public string ControlName
        {
            get => _ControlName;
            set
            {
                Set(ref _ControlName, value);

            }
        }
    
        private string _ControlName = string.Empty;

        /// <summary>
        /// 坐标系号
        /// </summary>
        public ushort CrdNo
        {
            get => _CrdNo;
            set => Set(ref _CrdNo, value);
        }
       
        private ushort _CrdNo;

        /// <summary>
        /// 轴数量
        /// </summary>
        public ushort AxisNum
        {
            get
            {
                if (AxisList.Length > 0)
                {
                    return (ushort)AxisList.Length;
                }
                return _AxisNum;
            }
            set => Set(ref _AxisNum, value);
        }      
        private ushort _AxisNum;

        /// <summary>
        /// 轴列表
        /// </summary>
        [JsonIgnore]
        public ushort[] AxisList 
        {
            get 
            {
                List<short> axisList = new List<short> { XAxisNo, YAxisNo, ZAxisNo, UAxisNo, VAxisNo, WAxisNo };

                // 使用LINQ的Where方法来过滤出大于0的元素，并转换为数组  
                var axisList2 = axisList
                .Where(axis => axis > 0) // 先过滤出大于0的元素               
               ; // 转换为ushort数组 
                List<ushort> axisList3 = new List<ushort>();
                foreach ( var axis in axisList2 )
                {
                    axisList3.Add((ushort)axis);
                }
                return axisList3.ToArray();
            }
            set { }
        }
      

        public short XAxisNo
        {
            get => _XAxisNo;
            set => Set(ref _XAxisNo, value);
        }
       
        private short _XAxisNo= -1 ;


        public short YAxisNo
        {
            get => _YAxisNo;
            set => Set(ref _YAxisNo, value);
        }
        
        private short _YAxisNo = -1;


        public short ZAxisNo
        {
            get => _ZAxisNo;
            set => Set(ref _ZAxisNo, value);
        }
       
        private short _ZAxisNo = -1;


        public short UAxisNo
        {
            get => _UAxisNo;
            set => Set(ref _UAxisNo, value);
        }
       
        private short _UAxisNo = -1;


        public short VAxisNo
        {
            get => _VAxisNo;
            set => Set(ref _VAxisNo, value);
        }
     
        private short _VAxisNo = -1;


        public short WAxisNo
        {
            get => _WAxisNo;
            set => Set(ref _WAxisNo, value);
        }
     
        private short _WAxisNo = -1;


        /// <summary>
        /// 最小速度
        /// </summary>
        public double MinVelocity
        {
            get => _MinVelocity;
            set => Set(ref _MinVelocity, value);
        }
       
        private double _MinVelocity = 1;

        /// <summary>
        /// 最大速度
        /// </summary>
        public double MaxVelocity
        {
            get => _MaxVelocity;
            set => Set(ref _MaxVelocity, value);
        }
       
        private double _MaxVelocity = 200;

        /// <summary>
        /// 加速度
        /// </summary>
        public double Acc
        {
            get => _Acc;
            set => Set(ref _Acc, value);
        }
       
        private double _Acc = 0.05;

        /// <summary>
        /// 减速度
        /// </summary>
        public double Dec
        {
            get => _Dec;
            set => Set(ref _Dec, value);
        }
        
        private double _Dec = 0.05;

        /// <summary>
        /// 停止速度
        /// </summary>
        public double StopVelocity
        {
            get => _StopVelocity;
            set => Set(ref _StopVelocity, value);
        }
       
        private double _StopVelocity;

        /// <summary>
        /// S曲线模式
        /// </summary>
        public ushort SMode
        {
            get => _SMode;
            set => Set(ref _SMode, value);
        }
        
        private ushort _SMode = 0;

        /// <summary>
        /// S曲线时间
        /// </summary>
        public double SPara
        {
            get => _SPara;
            set => Set(ref _SPara, value);
        }
    
        private double _SPara;


        #endregion

        [JsonIgnore]
        public ICommand? ApplyCommand
        {
            get => _ApplyCommand;
            set => Set(ref _ApplyCommand, value);
        }
        [JsonIgnore]
        private ICommand? _ApplyCommand;

        [JsonIgnore]
        public ICommand? ApplyAndSaveCommand
        {
            get => _ApplyAndSaveCommand;
            set => Set(ref _ApplyAndSaveCommand, value);
        }
        [JsonIgnore]
        private ICommand? _ApplyAndSaveCommand;




        public ImpMotionParaModel()
        {
            ApplyCommand = new Command(Apply);

            ApplyAndSaveCommand = new Command(ApplyAndSave);

            List<short> axisList = new List<short> { XAxisNo,YAxisNo,ZAxisNo,UAxisNo,VAxisNo,WAxisNo };

            // 使用LINQ的Where方法来过滤出大于0的元素，并转换为数组  
            AxisList = axisList
            .Where(axis => axis > 0) // 先过滤出大于0的元素  
            .Cast<ushort>() // 再进行类型转换（这里假设没有负数，所以不会抛出异常）  
            .ToArray(); // 转换为ushort数组 


        }

        /// <summary>
        /// 应用
        /// </summary>
        /// <param name="O"></param>
        public void Apply(Object O)
        {
            if (CrdEntity is null)
            {
                return;
            }
            Common.AutoMapProperties(this, CrdEntity);
            Common.AutoMapProperties(this, CrdEntity.ImputationConfig);
            CrdEntity.SetImpProfile();
            Toast.Show("应用成功!", targetWindow: ToastWindow.MainWindow);
        }

        /// <summary>
        /// 应用并保存
        /// </summary>
        /// <param name="O"></param>
        public void ApplyAndSave(Object O)
        {
            if (CrdEntity is null)
            {
                return;
            }

            Task.Run(() =>
            {
                Common.AutoMapProperties(this, CrdEntity);
                Common.AutoMapProperties(this, CrdEntity.ImputationConfig);

                CrdEntity.SetImpProfile();


            });

            

            SaveConfigData.SaveConfig<ImpMotionParaModel>(Paths.ImpMotionConfigPaths, this);

            //String path = Paths.ImpMotionConfigPaths;

            // var ImpMotionParaInfolList = ConvertJson.ReadFileToList<ImpMotionParaModel>(path);

            ////创建轴配置保存类
            //ImpMotionParaModel ImpMotionParaInfo = new ImpMotionParaModel();
            ////从当前类映射信息至配置类
            //Common.AutoMapPropertiesNotInterface(this, ImpMotionParaInfo);

            //if (ImpMotionParaInfolList != null)
            //{
            //    foreach (var item in ImpMotionParaInfolList)
            //    {
            //        if (item.ControlName is null)
            //        {
            //            return;
            //        }
            //        if (item.ControlName.Equals(this.ControlName))
            //        {
            //            ImpMotionParaInfolList[ImpMotionParaInfolList.IndexOf(item)] = ImpMotionParaInfo;
            //            ConvertJson.WriteJsonToFile(path, ImpMotionParaInfolList);
            //            Toast.Show("应用成功，已保存", targetWindow: ToastWindow.MainWindow);
            //            return;

            //        }

            //    }
            //    ImpMotionParaInfolList.Add(ImpMotionParaInfo);
            //    ConvertJson.WriteJsonToFile(path, ImpMotionParaInfolList);
            //    Toast.Show("未找到此轴号配置，已创建并写入成功！", targetWindow: ToastWindow.MainWindow);
            //}
            //else
            //{

            //    ImpMotionParaInfolList = new List<ImpMotionParaModel>
            //    {
            //        ImpMotionParaInfo,
            //    };
            //    ConvertJson.WriteJsonToFile(path, ImpMotionParaInfolList);
            //    Toast.Show("未找到配置文件，已创建并写入成功！", targetWindow: ToastWindow.MainWindow);
            //}


        }

        public void MappingFromEntity()
        {
            if (CrdEntity is null)
            {
                return;
            }
            CrdEntity.ImputationConfig.ControlName = CrdEntity.ControlName;
            //  Common.AutoMapPropertiesNotInterface(MotionEntity, this);
            Common.AutoMapPropertiesNotInterface(CrdEntity.ImputationConfig, this);
        }
    }
}
